Socket for LWIP Learning (API)

您所在的位置:网站首页 lwip socket api Socket for LWIP Learning (API)

Socket for LWIP Learning (API)

2023-12-14 12:50| 来源: 网络整理| 查看: 265

Socket API 1.Socket()

Interface Prototype

int lwip_socket(int domain, int type, int protocol) Functional action:

Function used to allocate a socket's descriptor and resources based on a specified address family, data type, and protocol

Function parameters:

domain address family AF_INET (IP_V4), AF_INET6(IP_V6) Typee supports three protocol types, Socket protocol types (TCP/UDP/RAW)

#Define SOCK_ STREAM 1 //Streaming Sockets provide reliable, connection-oriented traffic; It uses TCP protocol, which ensures the correctness and sequentiality of data transmission. #Define SOCK_ The DGRAM 2//datagram socket defines a connectionless service in which data is transmitted through separate messages, is out of order, and is not guaranteed to be reliable and error-free. It uses the datagram protocol UDP. #Define SOCK_ RAW 3//Raw sockets allow direct access to underlying protocols such as IP or ICMP. They are powerful but inconvenient to use and are mainly used for the development of some protocols.

protocol The protocol used by the system is defined as follows

#define IPPROTO_IP 0 #define IPPROTO_ICMP 1 #define IPPROTO_TCP 6 #define IPPROTO_UDP 17

You can set the protocol value to 0 and the system will automatically deduce what protocol should be used. So we usually write 0.

Return value

Create Error Return-1 Create Return Index Number Successfully

2.blind()

Interface prototype blind

int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen) Functional action:

Binding some properties (IP address, port number, address cluster) described in the sockaddr structure to socket sockets is also called socket naming.

Function parameters:

int s:socket() returns the socket represented by the file descriptor const struct sockaddr *name: Stores the address and port used by the server for communication. The sockaddr structure is as follows:

struct sockaddr { u8_t sa_len; u8_t sa_family; char sa_data[14]; };

The actual structure of this parameter depends on the family of network protocols, then passes in API functions with a forced conversion to SOCKADDR type. For example, AF_INET will use sockaddr_ The in structure acts as the actual structure. AF_INET6 uses sockaddr_ The in6 structure acts as the actual structure. Then cast to const struct sockaddr *name to resolve compiled warning errors.

socklen_t namelen Length of the previous parameter structure.

Return value

Successfully returned 0; Error return-1; The cause of the error is saved in errno. (typically address binding error, port occupied)

3.listen()

Interface Prototype

int lwip_listen(int s, int backlog) Functional action:

The listen function uses an active connection socket interface to become a connected socket interface, which allows a process to accept requests from other processes, thus becoming a server process.

Function parameters:

int s:socket() returns the socket represented by the file descriptor backlog: The len gt h of the listening queue, when the number of connections > this value, the client's request for a connection is rejected

Return value

Successfully returned 0; Error return-1;

4.accept()

Interface Prototype

int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) Functional action:

Extracts the first connection request from the waiting connection queue of the socket being listened for, creates a new socket, and returns a file descriptor pointing to the socket. Typically used to block processes in a server and wait for clients to connect.

Function parameters:

And above blind() Consistent entry of.

Return value

Returns the file descriptor pointing to the socket. Failure Return: -1; The cause of the error is saved in errno.

5.connect()

Interface Prototype

int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen) Functional action:

Used to establish a connection to the specified socket. Typically for clients,

Function parameters:

And above blind() Consistent entry of.

Return value

Successful return: 0 Failure Return: -1; The cause of the error is saved in errno.

6. Read/write read()/write(), recv()/send()

The following are the source code for read() and write() functions in lwip, except for recvfrom() and send(). read() function definition:

int lwip_read(int s, void *mem, size_t len) { return lwip_recvfrom(s, mem, len, 0, NULL, NULL); }

write() function definition:

int lwip_write(int s, const void *data, size_t size) { return lwip_send(s, data, size, 0); }

recv() function definition:

int lwip_recv(int s, void *mem, size_t len, int flags) { return lwip_recvfrom(s, mem, len, flags, NULL, NULL); }

send() function definition:

int lwip_send(int s, const void *data, size_t size, int flags) Function parameters:

int s: Socket represented by the destination connection file descriptor const void *data: pointer to send data void *mem: Memory address to receive data int flags: flags

7.close()

Interface Prototype

int lwip_close(int s) Functional action:

Close socket connection

Function parameters:

int s: Socket represented by a file descriptor

Return value

Successful return: 0 Failed Return: -1

Start with a brief introduction to common socket api functions, including select(), lwip_ After shutdown (). The source code for each api is then analyzed in detail. Enhance understanding. Where there are mistakes in the text, please ask the big guys to point out



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3